package com.xt.favorites.admin.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

//import com.favorites.domain.Favorites;
//import com.favorites.service.ConfigService;
//import com.favorites.service.FavoritesService;
import com.xt.common.utils.*;
import com.xt.favorites.admin.entity.Favorites;
import com.xt.favorites.admin.entity.FavoritesEntity;
import com.xt.favorites.admin.service.ConfigService;
import com.xt.favorites.admin.service.FavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xt.favorites.admin.entity.UserEntity;
import com.xt.favorites.admin.service.UserService;

import javax.annotation.Resource;


/**
 * 
 *
 * @author xt
 * @email xt@gmail.com
 * @date 2023-03-02 21:02:36
 */
@RestController
@RequestMapping("admin/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Resource
    private FavoritesService favoritesService;
    @Resource
    private ConfigService configService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("admin:user:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     *获取用户总量+日注册量
     * @return
     */
    @RequestMapping("/console")
    public R console(){
        List<Map<String,Integer>> data =  userService.console();
        return R.ok().put("data", data);

    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("admin:user:info")
    public R info(@PathVariable("id") Long id){
		UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存新增,注册
     */
    @RequestMapping("/save")
    //@RequiresPermissions("admin:user:save")
    public R save(@RequestBody UserEntity user){

        //1、判断邮箱是否被注册
        UserEntity registUser = userService.findByEmail(user.getEmail());
        if (null != registUser) {
            return R.error("该邮箱已经被注册");
        }
        //2、判断用户名是否被注册
        UserEntity userNameUser = userService.findByUserName(user.getUserName());
        if (null != userNameUser) {
            return R.error("该用户名已经被注册");
        }

        user.setCreateTime(DateUtils.getCurrentTime());
        user.setLastModifyTime(DateUtils.getCurrentTime());
        user.setBackgroundPicture(null);
        user.setOutDate(DateUtils.getDateFormat());
        //user.setProfilePicture("img/favicon.png");
        user.setPassWord(MD5Util.encrypt(user.getPassWord()+ Const.PASSWORD_KEY));
		userService.save(user);
        // 添加默认收藏夹
        String unReadList = "未读列表";
        FavoritesEntity favorites = favoritesService.saveFavorites(user.getId(), unReadList);
        // 添加默认属性设置
        configService.saveConfig(user.getId(),String.valueOf(favorites.getId()));

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("admin:user:update")
    public R update(@RequestBody UserEntity user){
        user.setPassWord(MD5Util.encrypt(user.getPassWord()+ Const.PASSWORD_KEY));
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("admin:user:delete")
    public R delete(@RequestBody Long[] ids){
		userService.removeByIdsAll(Arrays.asList(ids));

        return R.ok();
    }

}
